/*
@title: balloon
@description: "Balloon" is a simple maze game where you navigate a balloon through various levels, pushing other balloons out of the way to reach the goal.
@author: Lucas
@tags: []
@addedOn: 2022-09-08
*/

const player = "p";
const obj1 = "8";
const obj2 = "2";
const prego = "o";
const final = "f";

setLegend(
  [ player, bitmap`
................
................
.......555......
......55555.....
......55555.....
......55555.....
......55555.....
.......555......
........5.......
........5.......
.........5......
........5.......
.......5........
........5.......
.........5......
................`],
  [ prego, bitmap`
.0..0......0....
...0.........0..
....0..0..0.....
.0..............
....0.0..0.0..0.
0.0.............
..0...0.0....0..
..0..0..0.......
..0.......00..0.
0...0.0..0....0.
0.0......0..0...
................
....0..0...0..0.
.0..0...........
...........0....
........0.......`],
  [ final, bitmap`
................
................
................
................
...666666666....
...666666666....
...666666666....
...666666666....
...666666666....
...666666666....
...666666666....
...666666666....
...666666666....
................
................
................`],
  [ obj1, bitmap`
................
................
.......777......
......77777.....
......77777.....
......77777.....
......77777.....
.......777......
........7.......
........7.......
.........7......
..........7.....
.........7......
........7.......
................
................`],
  [ obj2, bitmap`
................
................
........888.....
.......88888....
.......88888....
.......88888....
.......88888....
........888.....
.........8......
.........8......
..........8.....
...........8....
..........8.....
.........8......
................
................`],
);
  
let level = 0;
const levels = [
  map`
po.....
.o.2.8.
...o...
..of...`,
  map`
pof....
.oooo..
..28...
o......`,
  map`
p.ofo.o
..o....
..ooo2.
.....8.`,
];
const faseAtual = levels[level];
setMap(faseAtual);

setMap(levels[level]);

setPushables({
  [ player ]: [ obj1, obj2 ],
});

onInput("w", () => {
  getFirst(player).y -= 1;
});

onInput("s", () => {
  getFirst(player).y += 1;
});

onInput("a", () => {
  getFirst(player).x -= 1;
});

onInput("d", () => {
  getFirst(player).x += 1;
});

onInput("j", () => {
  const faseAtual = levels[level];
  if (faseAtual !== undefined) setMap(faseAtual);
});

setSolids([prego, player, obj1, obj2 ]);

afterInput(() => {
  const atual = tilesWith( player, final).length;
  const f2 = tilesWith(final).length;
   if (atual === f2) {
    level = level + 1;

    const faseAtual = levels[level];

    if (faseAtual !== undefined) {
      setMap(faseAtual);
    } else {
      addText("you win!")
    }
  }
});
